﻿<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
        <!--用户数据-->
        <pane size="84">
          <el-col>
            <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
              label-width="68px">
              <el-form-item label="手机号码" prop="phonenumber">
                <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号码" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="支付状态" clearable style="width: 240px">
                  <el-option v-for="dict in dict.type.order_state" :key="dict.value" :label="dict.label"
                    :value="dict.value" />
                </el-select>
              </el-form-item>

              <el-form-item label-width="120px" label="注册时间" prop="daterange">
                <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期"
                  end-placeholder="结束日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form> -->

            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openAdd">新增
                </el-button>
              </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="userList">
              <el-table-column type="selection" width="50" align="center" />
              <el-table-column label="名称" align="center" prop="name" />
              <el-table-column label="基数" align="center" prop="cardinality">
                <template slot-scope="scope">
                  {{ scope.row.cardinality }}%
                </template>
              </el-table-column>
              <el-table-column label="封顶" align="center" prop="capping">
                <template slot-scope="scope">
                  {{ scope.row.capping }}%
                </template>
              </el-table-column>
              <el-table-column label="最低人数配置" align="center" prop="minPeopleNum" />
              <el-table-column label="增长比例" align="center" prop="increase">
                <template slot-scope="scope">
                  {{ scope.row.increase }}%
                </template>
              </el-table-column>
              <el-table-column label="备注" align="center" prop="remark" />
              <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
                <template slot-scope="scope" v-if="scope.row.userId !== 1">
                  <el-button size="mini" type="text" icon="el-icon-edit" @click="openEdit(scope.row)">编辑
                  </el-button>
                  <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDel(scope.row)">删除
                  </el-button> -->
                </template>
              </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize" @pagination="getList" />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="dialogShow" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="name">
          <div class="ub ub-ac ub-js">
            <el-input style="width: 300px;" v-model="form.name" placeholder="请输入名称" />
          </div>
        </el-form-item>
        <el-form-item label="基数" prop="cardinality">
          <div class="ub ub-ac ub-js">
            <el-input style="width: 300px;" v-model="form.cardinality" placeholder="请输入基数" type="number" /> %
          </div>
        </el-form-item>
        <el-form-item label="封顶" prop="capping">
          <div class="ub ub-ac ub-js">
            <el-input style="width: 300px;" v-model="form.capping" placeholder="请输入封顶" type="number" /> %
          </div>
        </el-form-item>
        <el-form-item label="最低人数配置" prop="minPeopleNum">
          <div class="ub ub-ac ub-js">
            <el-input style="width: 300px;" v-model="form.minPeopleNum" placeholder="请输入最低人数配置" type="number" />
          </div>
        </el-form-item>
        <el-form-item label="增长比例" prop="increase">
          <div class="ub ub-ac ub-js">
            <el-input style="width: 300px;" type="number" v-model="form.increase" placeholder="请输入增长比例" /> %
          </div>
        </el-form-item>
        <el-form-item label="备注">
          <div class="ub ub-ac ub-js">
            <el-input style="width: 300px;" v-model="form.remark" placeholder="请输入备注" rows="3" type="textarea" />
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  promotionList,
  promotionEdit,
  promotionAdd
} from "@/api/promotion/index";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";

export default {
  name: "User",
  dicts: ["user_type", "sys_normal_disable", "sys_user_sex", "order_state"],
  components: { Splitpanes, Pane },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {
        name: undefined,
        cardinality: undefined, //分成基数比例
        minPeopleNum: undefined,//最低人数配置
        capping: undefined,// 分成封顶比例
        increase: undefined,// 增长比例
        decrease: undefined,// 减少比例
        remark: undefined

      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        cardinality: [
          { required: true, message: "分成基数比例不能为空", trigger: "blur" },
          { validator: this.validateCardinality, trigger: 'blur' }
        ],
        minPeopleNum: [
          { required: true, message: "最低人数配置不能为空", trigger: "blur" },
          { validator: this.validateMinPeopleNum, trigger: 'blur' }
        ],
        capping: [
          { required: true, message: "分成封顶比例不能为空", trigger: "blur" },
          { validator: this.validateCapping, trigger: 'blur' }
        ],
        increase: [
          { required: true, message: "增长比例不能为空", trigger: "blur" },
          { validator: this.validateIncrease, trigger: 'blur' }
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'createTime',
        isAsc: 'descending',
        phoneNumber: undefined,
        status: undefined
      },

      // 新增修改弹窗
      dialogShow: false,
      title: '',

    };
  },
  watch: {
  },
  created() {
    this.getList();
    // this.getConfigKey("sys.user.initPassword").then((response) => {
    //   this.initPassword = response.msg;
    // });
  },
  methods: {
    validateCardinality(rule, value, callback) {
      if (value < 10) {
        callback(new Error('分成基数必须大于等于10%'));
      } else if (value > 70) {
        callback(new Error('分成基数必须小于等于70%'));
      } else {
        callback();
      }
    },
    validateIncrease(rule, value, callback) {
      if (value < 1) {
        callback(new Error('增长比例必须大于等于1%'));
      } else if(value > 5) {
        callback(new Error('增长比例不能大于5%'));
      }else {
        callback();
      }
    },
    validateMinPeopleNum(rule, value, callback) {
      if (value < 5) {
        callback(new Error('最低人数配置必须大于等于5'));
      } else {
        callback();
      }
    },
    validateCapping(rule, value, callback) {
      const { cardinality, capping } = this.form;
      if (!value || !cardinality) {
        callback(new Error('分成基数和封顶比例都必须有值'));
      } else if (parseInt(value) < cardinality) {
        callback(new Error('封顶比例必须大于等于分成基数'));
      } else if (value > 70) {
        callback(new Error('封顶比例必须小于等于70%'));
      } else {
        callback();
      }
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      promotionList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.dialogShow = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        phoneNumber: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    handleEdit(row) {

    },
    /** 删除按钮操作 */
    handleDel(row) {
      this.$modal
        .confirm('是否删除改数据？')
        .then(function () {
          // return orderRefund({
          //   userId: row.userId,
          //   orderNo: row.orderNo,
          // });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },

    openAdd() {
      this.title = "新增配置";
      this.dialogShow = true;
    },
    openEdit(row) {
      this.title = "修改配置";
      this.form = row;
      this.dialogShow = true;
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            promotionEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.dialogShow = false;
              this.getList();
            });
          } else {
            promotionAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.dialogShow = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
